package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.*;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

   @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(dto,cart);//将传入的数据拷贝到cart对象中

        //1.判断商品是否存在
        cart.setUserId(BaseContext.getCurrentId());//只能查询当前用户的购物车信息
        ShoppingCart shoppingCart= shoppingCartMapper.selByDtoId(cart);

        if(shoppingCart == null){               //不存在，则添加商品到购物车
            //判断是添加商品还是套餐
            if (cart.getDishId() != null){   //有商品ID，则为添加商品
                //补充cart剩下的属性
                Dish dish = dishMapper.selectById(cart.getDishId());

                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());
            }else {                         //没有商品ID，则为添加套餐
                //补充cart剩下的属性
                SetmealVO setmeal = setmealMapper.selByid(cart.getSetmealId());

                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setAmount(setmeal.getPrice());
            }
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            //调用mapper方法插入数据
            shoppingCartMapper.insert(cart);

        }else {                                 //存在，则修改商品数量
           shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumber(shoppingCart);//修改商品的数量加1
        }

    }

    /**
     * 查看购物车
     * @return List<ShoppingCart>
     */
    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> list= shoppingCartMapper.list(BaseContext.getCurrentId());
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //根据当前用户的id，清除自己的购物车数据
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中的商品
     * @param dto
     */
    @Transactional//开启事务
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(dto,cart);//拷贝 数据
        cart.setUserId(BaseContext.getCurrentId());//只能查询当前用户的购物车信息
        //查询要修改的具体数据
        ShoppingCart shoppingCart = shoppingCartMapper.selByDtoId(cart);

        //判断商品数量是否为1
        if (shoppingCart.getNumber()==1) {          //为1执行删除操作
            shoppingCartMapper.delete(cart);

        }else {                                     //不为1执行修改操作
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateNumber(shoppingCart);
        }



    }
}
